package com.zsrt.exam.service;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zsrt.exam.entity.UserEntity;
import com.zsrt.exam.utils.Util;
public class UserService  {
	
	
	/**
	 * 拿到一个系统管理员
	 * @return
	 * @throws NoSuchAlgorithmException 
	 */
	public UserEntity getAdmin() throws NoSuchAlgorithmException{
		
		UserEntity admin =new UserEntity();
		admin.setName("1");
		admin.setPassword(Util.stringMD5("123"));
		
		//关键代码
		admin.setRole("admin");
		return admin;
	}
	
	/**
	 * 得到所有学生
	 * @return
	 * @throws NoSuchAlgorithmException 
	 */
	public List<UserEntity> getAllUsers() throws NoSuchAlgorithmException{
		
		List<UserEntity> list=new ArrayList<UserEntity>();
		
		UserEntity user01 = new UserEntity();
		user01.setCard("1");
		user01.setName("刘浩");
		
		user01.setPassword(Util.stringMD5("123"));
		user01.setAge(24);
		user01.setProvince("广东省");
		user01.setCity("茂名");
		user01.setPhone("15766472116");
		user01.setChinese(100);
		user01.setMath(100);
		user01.setEnghish(100);
		user01.setOther(205);
		user01.setRole("user");
		
		UserEntity user02 = new UserEntity();
		user02.setCard("2");
		user02.setName("张志东");
		user02.setPassword(Util.stringMD5("123"));
		user02.setAge(24);
		user02.setProvince("广东省");
		user02.setCity("茂名");
		user02.setPhone("15766516891");
		user02.setChinese(100);
		user02.setMath(100);
		user02.setEnghish(100);
		user02.setOther(201);
		user02.setRole("user");
		
		UserEntity user03 = new UserEntity();
		user03.setCard("3");
		user03.setName("黄志杰");
		user03.setPassword("123456");
		user03.setAge(24);
		user03.setProvince("广东省");
		user03.setCity("茂名");
		user03.setPhone("15766518639");
		user03.setChinese(100);
		user03.setMath(100);
		user03.setEnghish(100);
		user03.setOther(210);
		user03.setRole("user");
		
		UserEntity user04 = new UserEntity();
		user04.setCard("4");
		user04.setName("陈皋");
		user04.setPassword("123456");
		user04.setAge(24);
		user04.setProvince("湖南省");
		user04.setCity("邵阳市");
		user04.setPhone("18617067316");
		user04.setChinese(100);
		user04.setMath(100);
		user04.setEnghish(100);
		user04.setOther(260);
		user04.setRole("user");
		
		UserEntity user05 = new UserEntity();
		user05.setCard("5");
		user05.setName("钟鹏");
		user05.setPassword("123456");
		user05.setAge(24);
		user05.setProvince("湖南省");
		user05.setCity("邵阳市");
		user05.setPhone("15766472116");
		user05.setChinese(100);
		user05.setMath(300);
		user05.setEnghish(100);
		user05.setOther(220);
		user05.setRole("user");
		
		UserEntity user06 = new UserEntity();
		user06.setCard("6");
		user06.setName("段誉");
		user06.setPassword("123456");
		user06.setAge(24);
		user06.setProvince("云南省");
		user06.setCity("大理市");
		user06.setPhone("15766472116");
		user06.setChinese(100);
		user06.setMath(100);
		user06.setEnghish(200);
		user06.setOther(220);
		user06.setRole("user");
		
		
		list.add(user01);
		list.add(user02);
		list.add(user03);
		list.add(user04);
		list.add(user05);
		list.add(user06);
		
		return list;
		
	}

	/**
	 * 登陆的方法
	 */
	public void login(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          1.管理员登录                 ");
		System.err.println("\t\t          2.普通会员登录              ");
		System.err.println("\t\t          3. 退   出                       ");
		System.err.println("\t\t —————————————————————————————");
		
	}
	
	//管理员页面
	public void enterAdminMenu() {
		
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          11 录入考生信息                 ");
		System.err.println("\t\t          12 修改考生信息             ");
		System.err.println("\t\t          13 删除考生信息                       ");
		System.err.println("\t\t          14 查询考生信息                       ");
		System.err.println("\t\t          15 查询所有考生信息");
		System.err.println("\t\t          16 返回上层");
		System.err.println("\t\t —————————————————————————————");
		
	}
	
	/**
	 * 添加一个学生
	 * @param name 名字
	 * @param card 准考证号
	 * @param other 综合
	 * @return
	 */
	public UserEntity addUser(String name, String card, int other) {
		UserEntity user04 = new UserEntity();
		user04.setCard(card);
		user04.setName(name);
		user04.setPassword("123456");
		user04.setAge(24);
		user04.setProvince("湖南省");
		user04.setCity("邵阳市");
		user04.setPhone("18617067316");
		user04.setChinese(126);
		user04.setMath(117);
		user04.setEnghish(98);
		user04.setOther(other);
		user04.setRole("user");
		
		return user04;
	}
		
	
	/**
	 * 删除学生信息
	 * @param cardNum 准考证号码
	 * @return 是否删除成功
	 */
	public Boolean delete(String cardNum,List<UserEntity> userList){
		
		boolean falg=false;
		
		//先要查找到
		for(int i=0;i<userList.size();i++){
			String num = userList.get(i).getCard();
			if(cardNum.equals(num)){
				userList.remove(i);
				falg=true;
			}
		}
		return falg;
	}
	
	/**
	 * 修改一个学生的省 和city
	 * @param keyWork  准考证号
	 * @param userList 学生列表
	 * @param province 省份
	 * @param city     城市
	 * @return
	 */
	public boolean updete(String num,List<UserEntity> userList,String province,String city){
		
		//按准考证号修改
				boolean falg=false;
			
				//先要查找到
				for(int i=0;i<userList.size();i++){
					String card = userList.get(i).getCard();
					if(num.equals(card)){
						userList.get(i).setProvince(province);
						userList.get(i).setCity(city);
						falg=true;
					}
				}
				return falg;
	}

	
	/**
	 * 1 根据城市查询
	 *2 根据姓名查询
	 *3 根据准考证号码查询
	 * 4 返回上一层
	 * 
	 * 
	 * 
	 * @param no  查询代码
	 * @param keyWord 关键字
	 */
	public List<UserEntity> findBykeyWord(int no,String keyWord,List<UserEntity> userList){
		
		List<UserEntity> newUserList=new ArrayList<UserEntity>();
		
		//根据城市查询
		if(no==1){
			for(int i=0;i<userList.size();i++){
				
				//城市等于关键字
				if(keyWord.equals(userList.get(i).getCity())){
					newUserList.add(userList.get(i));
				}
			}
			
		//根据姓名查询
		}else if(no==2){
				for(int i=0;i<userList.size();i++){
				
				//名字等于关键字
				if(keyWord.equals(userList.get(i).getName())){
					newUserList.add(userList.get(i));
				}
			}
				
		//根据准考证号码查询	
		}else if(no==3){
			for(int i=0;i<userList.size();i++){
				
				//准考证号码等于关键字
				if(keyWord.equals(userList.get(i).getCard())){
					newUserList.add(userList.get(i));
				}
			}
		}
		
		return newUserList;
	}
	
	/**
	 * 个人信息包括分数
	 * @param cardNum 准考证号码
	 */
	public void seeOneself(String cardNum,List<UserEntity> userList){
		
		for(int i=0;i<userList.size();i++){
			
			//准考证号码
			if(cardNum.equals(userList.get(i).getCard())){
				System.out.println(userList.get(i));
				break;
			}else{
				System.out.println("不存在这准考证号");
			}
		}
	}

	
	//查询本市(登录用户所在的城市)的总分状元
		/**
		 * 
		 * @param num 登陆的考生准考证号
		 * @return 当前城市的总分状元
		 */
		public UserEntity cityTop1(String num,List<UserEntity> userList){
			
			String cityName=null;
			//有准考证号获得对应的城市
			for(int i=0;i<userList.size();i++){
				String name = userList.get(i).getCard();
				if(num.equals(name)){
					cityName=userList.get(i).getCity();
				}
			}
			
			//存放最高分的考生
			double maxMark=0;
			
			//考生的下标
			int xb=-1;
			
			//对同一城市的考生总分进行比较
			for(int i=0;i<userList.size();i++){
				
				UserEntity u=userList.get(i);
				
				if(cityName.equals(u.getCity())){
					
					//同一城市下考生总分
					double total=u.getChinese()+u.getMath()+u.getEnghish()+u.getOther();
					
					if(total>maxMark){
						maxMark=total;
						xb++;
					}
				}
			}
			if(xb==-1){
				System.out.println("考生成绩还没公布");
				return userList.get(0);
			}
			return userList.get(xb);
		}
		
		
		/**
		 * 1 查询各个省份500-600分的总人数并由高到低排列显示
		 * 
		 * 
		 */
		public UserEntity targerCityTop1(String cityName,List<UserEntity> userList){
			
			//存放最高分的考生
			double maxMark=0;
			
			//考生的下标
			int xb=-1;
			
			//对同一城市的考生总分进行比较
			for(int i=0;i<userList.size();i++){
				
				UserEntity u=userList.get(i);
				
				if(cityName.equals(u.getCity())){
					
					//同一城市下考生总分
					double total=u.getChinese()+u.getMath()+u.getEnghish()+u.getOther();
					
					if(total>maxMark){
						maxMark=total;
						xb++;
					}
				}
			}
			if(xb==-1){
				System.out.println("考生成绩还没公布");
				return userList.get(0);
			}
			return userList.get(xb);
		}
		
		
		//查询全国各个省份的状元
		/**
		 * 各个省份的状元
		 */
		public void provinceTop(List<UserEntity> userList){
			
			//存放各省,利用不能重复的特点
			Set<String> proSet=new HashSet<String>();
			
			for(int i=0;i<userList.size();i++){
					proSet.add(userList.get(i).getProvince());
			}
			
			Iterator<String> it = proSet.iterator();
			while(it.hasNext()){
				
				String pro = it.next();
				double maxMark=0;
				int xb=-1;
				for(int i=0;i<userList.size();i++){
					if(pro.equals(userList.get(i).getProvince())){
						double total=userList.get(i).getChinese()+userList.get(i).getMath()+userList.get(i).getEnghish()+userList.get(i).getOther();
						if(total>maxMark){
							maxMark=total;
							xb=i;
						}
					}
				}
				
				//循环一次打印一个省的状元
				System.out.println(pro+"省的状元是："+userList.get(xb)+"总分："+maxMark);
			}
		}
		
		
		/**
		 * 1 查询各个省份500-600分的总人数并由高到低排列显示
		 * 
		 * 
		 */
		public void statistics(List<UserEntity> userList){
			
			//存放各省,利用不能重复的特点
			Set<String> proSet=new HashSet<String>();
			
			
			
			for(int i=0;i<userList.size();i++){
				proSet.add(userList.get(i).getProvince());
			}
			
			Iterator<String> it = proSet.iterator();
				while(it.hasNext()){
					String pro = it.next();
					
					List<UserEntity> newUser=new ArrayList<UserEntity>();
					
					for(int i=0;i<userList.size();i++){
						if(pro.equals(userList.get(i).getProvince())){
							double total=userList.get(i).getChinese()+userList.get(i).getMath()+userList.get(i).getEnghish()+userList.get(i).getOther();
							
							//赋值总分
							userList.get(i).setTotal(total);
							if(total>=500 && total<=600){
								
								//存放在新的数组   
								newUser.add(userList.get(i));
								
							}
						}
					}
					//排序  Collections.sort
					Collections.sort(newUser);
					
					//循环一次打印一个省的考生
					for (UserEntity user : newUser) {
						System.out.println(pro+"省的500到600分的考生："+user);
					}
					
				}
			
		}
		
		/**
		 * map 实现的那全省考生排名
		 * @param userList
		 * @return 排序后的各省的考生
		 * 
		 */
	public Map<String,List<UserEntity>> findAllUsersByProvince(List<UserEntity> userList){
	
		Map<String,List<UserEntity>> map=new HashMap<String,List<UserEntity>>();
		
		// 保存各个省
		Set<String> pro=new HashSet<String>();
		for (UserEntity s : userList) {
			pro.add(s.getProvince());
		}
		
		
		Iterator<String> it = pro.iterator();
		while(it.hasNext()){
			
			//保存大于600分的学生
			List<UserEntity> too600 = new ArrayList<UserEntity>();
			String province = it.next();
			
			for(int i=0;i<userList.size();i++){
				
				double total=userList.get(i).getChinese()+userList.get(i).getMath()+userList.get(i).getEnghish()+userList.get(i).getOther();
				
				//赋值总分
				userList.get(i).setTotal(total);
				
				if(province.equals(userList.get(i).getProvince()) && userList.get(i).getTotal()>600){
					
					//同一个省的考生,大于600分
					too600.add(userList.get(i));
				}
			}
			Collections.sort(too600);
			map.put(province, too600);
		}
		

		return map;
	}
	
	
}
